package com.cal.utms.dao.payment;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

import com.cal.utms.dto.payment.InvestmentForPledge;
import com.cal.utms.entity.CustomerPledge;
import com.cal.utms.entity.CustomerTransactionLog;
import com.cal.utms.util.HibernateUtil;

public class PledgeDaoImpl implements PledgeDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<InvestmentForPledge> getInvestmentByFund(int fundId, int customerId) {
		Session session = HibernateUtil.getSession().openSession();
		List<InvestmentForPledge> transactions = new ArrayList<InvestmentForPledge>();
		try {
			Criteria criteria = session.createCriteria(CustomerTransactionLog.class);
			criteria.add(Restrictions.eq("fund.id", fundId));
			criteria.add(Restrictions.eq("customer.id", customerId));
			criteria.setProjection(Projections.projectionList().add(Projections.property("customer"), "customer")
					.add(Projections.property("customerInvestment"), "customerInvestment").add(Projections.property("fund"), "fund")
					.add(Projections.sum("tranUnits"), "totalUnits").add(Projections.groupProperty("customerInvestment")));
			criteria.setResultTransformer(Transformers.aliasToBean(InvestmentForPledge.class));
			transactions = (List<InvestmentForPledge>) criteria.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return transactions;
	}

	public Object savePledges(List<CustomerPledge> customerPledges, List<CustomerTransactionLog> customerTransactionLogs) {
		Session session = HibernateUtil.getSession().openSession();
		Object object = null;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			for (CustomerPledge customerPledge : customerPledges) {
				object = session.save(customerPledge);
			}
			for (CustomerTransactionLog customerTransactionLog : customerTransactionLogs) {
				session.save(customerTransactionLog);
			}
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return object;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerPledge> getPledge(int fundId, int customerId) {
		Session session = HibernateUtil.getSession().openSession();
		List<CustomerPledge> customerPledges = null;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			customerPledges = session.createQuery("from CustomerPledge CP where CP.customer.id=:customerid and CP.fund.id=:fundId and CP.status=:stat")
					.setParameter("customerid", customerId).setParameter("fundId", fundId).setParameter("stat", "PLEDGE").list();
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return customerPledges;
	}

	public CustomerPledge getPladgeByid(int pledgeid) {
		Session session = HibernateUtil.getSession().openSession();
		CustomerPledge customerPledge = null;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			customerPledge = (CustomerPledge) session.get(CustomerPledge.class, pledgeid);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return customerPledge;
	}

	public Object updatePladge(CustomerPledge customerPledge, List<CustomerTransactionLog> customerTransactionLogs) {
		Session session = HibernateUtil.getSession().openSession();
		Object object = null;
		Transaction transaction = null;
		try {
			for (CustomerTransactionLog customerTransactionLog : customerTransactionLogs) {
				session.save(customerTransactionLog);
			}
			transaction = session.beginTransaction();
			object = session.merge(customerPledge);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}

}
